<?php

define('RUB', '1');
define('EUR', '2');
define('USD', '3');
define('MESSAGE_ACTIVE', '0');
define('MESSAGE_BASKET', '1');
define('MESSAGE_DELETED', '2');
define('LAYER1', '1');
define('LAYER2', '2');
define('LAYER3', '3');

class core{

	private $conect;
	
	function __construct(){
		$this->initVars();
		include 'class.user.php';	
		include 'class.admin.php';
		include 'class.database.php';
		include 'class.gamers.php';
		$users_obj = new users();
		$gamers_obj = new gamers();
		if(array_key_exists('login', $_POST) && array_key_exists('password', $_POST) && array_key_exists('confirm', $_POST)
			&& array_key_exists('name', $_POST) && array_key_exists('surname', $_POST) && array_key_exists('phone', $_POST)
			&& array_key_exists('email', $_POST) && array_key_exists('country', $_POST) && array_key_exists('city', $_POST)
			&& array_key_exists('question', $_POST) && array_key_exists('response', $_POST)){
			$userdata = array();
			$arr = array();
			$userdata['login'] = stripslashes(strip_tags(trim($_POST['login'])));
			$userdata['password'] = stripslashes(strip_tags(trim($_POST['password'])));			
			$userdata['confirm'] = stripslashes(strip_tags(trim($_POST['confirm'])));			
			$userdata['name'] = stripslashes(strip_tags(trim($_POST['name'])));
			$userdata['surname'] = stripslashes(strip_tags(trim($_POST['surname'])));
			$userdata['phone'] = stripslashes(strip_tags(trim($_POST['phone'])));
			$userdata['email'] = stripslashes(strip_tags(trim($_POST['email'])));
			$userdata['country'] = stripslashes(strip_tags(trim($_POST['country'])));
			$userdata['city'] = stripslashes(strip_tags(trim($_POST['city'])));
			$userdata['question'] = stripslashes(strip_tags(trim($_POST['question'])));
			$userdata['response'] = stripslashes(strip_tags(trim($_POST['response'])));			
			
			$arr["is_registered"] = $users_obj->is_registered($userdata['login']);
			if ($arr["is_registered"]){					
				return print json_encode($arr);	
			}
			$arr["is_email"] = 	$users_obj->is_email(trim( stripslashes($_POST['email'])));{
			if ($arr["is_email"])
				return print json_encode($arr);
			}			
			if ((!$users_obj->is_registered($userdata['login'])) && (!$users_obj->is_email($userdata['email']))){
				$confirm = $users_obj->confirm($userdata['login'],$userdata['name'],$userdata['surname'],$userdata['email']);			
				$users_obj->TemplateRegisterUser(
					array(
						'login' => $userdata['login'], 
						'password' => $userdata['password'],
						'confirm' => $userdata['confirm'],
						'name' => $userdata['name'],
						'surname' => $userdata['surname'],
						'phone' => $userdata['phone'],
						'email' => $userdata['email'],
						'country' => $userdata['country'],
						'city' => $userdata['city'],
						'question' => $userdata['question'],
						'response' => $userdata['response'],
						'confirm_reg' => $confirm
					)
				);
				$arr["send_message"] = $users_obj->send_message($userdata['confirm'],$userdata['email'],$userdata['login']);
				if ($arr["send_message"])
					return print json_encode($arr);	
				return false;
			}
		}
		
		if(array_key_exists('login', $_POST) && array_key_exists('password', $_POST) && array_key_exists('confirm', $_POST)
			&& array_key_exists('name', $_POST) && array_key_exists('surname', $_POST) && array_key_exists('phone', $_POST)
			&& array_key_exists('email', $_POST) && array_key_exists('country', $_POST) && array_key_exists('city', $_POST)
			&& array_key_exists('question', $_POST) && array_key_exists('response', $_POST) && array_key_exists('privilege')
		){
			$userdata = array();
			$arr = array();
			
			$userdata['login'] = stripslashes(strip_tags(trim($_POST['login'])));
			$userdata['password'] = stripslashes(strip_tags(trim($_POST['password'])));			
			$userdata['confirm'] = stripslashes(strip_tags(trim($_POST['confirm'])));			
			$userdata['name'] = stripslashes(strip_tags(trim($_POST['name'])));
			$userdata['surname'] = stripslashes(strip_tags(trim($_POST['surname'])));
			$userdata['phone'] = stripslashes(strip_tags(trim($_POST['phone'])));
			$userdata['email'] = stripslashes(strip_tags(trim($_POST['email'])));
			$userdata['country'] = stripslashes(strip_tags(trim($_POST['country'])));
			$userdata['city'] = stripslashes(strip_tags(trim($_POST['city'])));
			$userdata['question'] = stripslashes(strip_tags(trim($_POST['question'])));
			$userdata['response'] = stripslashes(strip_tags(trim($_POST['response'])));
			$userdata['privilege'] = stripslashes(strip_tags(trim($_POST['privilege'])));
		
		
			$arr["is_registered"] = $users_obj->is_registered($userdata['login']);
			if ($arr["is_registered"]){					
				return print json_encode($arr);	
			}
			$arr["is_email"] = 	$users_obj->is_email($userdata['email']);{
			if ($arr["is_email"])
				return print json_encode($arr);
			}
			
			if (!$users_obj->is_registered($userdata['login']) && ((!$users_obj->is_email($userdata['email'])))){
				$confirm = $users_obj->confirm($userdata['login'],$userdata['name'],$userdata['surname'],$userdata['email']);			
				$admin_obj->TemplateAddUser(
					array(
						'login' => $userdata['login'], 
						'password' => $userdata['password'],
						'confirm' => $userdata['confirm'],
						'name' => $userdata['name'],
						'surname' => $userdata['surname'],
						'phone' => $userdata['phone'],
						'email' => $userdata['email'],
						'country' => $userdata['country'],
						'city' => $userdata['city'],
						'question' => $userdata['question'],
						'response' => $userdata['response'],
						'confirm_reg' => $confirm,
						'privilege' => $userdata['privilege']
					)
				);
				$arr["send_message"] = $users_obj->send_message($confirm,$userdata['email'],$userdata['login']);
				if ($arr["send_message"])
					return print json_encode($arr);	
			}
		}
		
		if(array_key_exists('name_upd', $_POST) && array_key_exists('surname_upd', $_POST) && array_key_exists('phone_upd', $_POST)
			&& array_key_exists('country_upd', $_POST) && array_key_exists('city_upd', $_POST)
			&& array_key_exists('question_upd', $_POST) && array_key_exists('response_upd', $_POST)){
			$userdata = array();
			$arr = array();
			
			$userdata['name_upd'] = stripslashes(strip_tags(trim($_POST['name_upd'])));
			$userdata['surname_upd'] = stripslashes(strip_tags(trim($_POST['surname_upd'])));
			$userdata['phone_upd'] = stripslashes(strip_tags(trim($_POST['phone_upd'])));
			$userdata['country_upd'] = stripslashes(strip_tags(trim($_POST['country_upd'])));
			$userdata['city_upd'] = stripslashes(strip_tags(trim($_POST['city_upd'])));
			$userdata['question_upd'] = stripslashes(strip_tags(trim($_POST['question_upd'])));
			$userdata['response_upd'] = stripslashes(strip_tags(trim($_POST['response_upd'])));
			
			
			(int)$user_id = $_SESSION['user']['id'];			
			$arr["update_user"] = $users_obj->update_user(					
						array(
						'id' => $user_id,
						'login' => $_SESSION['user']['login'],				
						'name' => $userdata['name_upd'],
						'surname' => $userdata['surname_upd'],
						'phone' => $userdata['phone_upd'],
						'email' => $_SESSION['user']['email'],
						'country' => $userdata['country_upd'],
						'city' => $userdata['city_upd'],
						'question' => $userdata['question_upd'],
						'response' => $userdata['question_upd'],
						'privilege' => $_SESSION['user']['privilege']
					)				
				);	
			
			return print json_encode($arr);	 
		}
		
		if(array_key_exists('login_auth', $_POST) && array_key_exists('password_auth', $_POST)){	
			$userdata = array();
			$arr = array();
			
			$userdata['login_auth'] = stripslashes(strip_tags(trim($_POST['login_auth'])));
			$userdata['password_auth'] = stripslashes(strip_tags(trim($_POST['password_auth'])));
			
			if(!$users_obj->auth($userdata['login_auth'],$userdata['password_auth'])){
				$arr['auth'] = false;
			}
			else
				$arr['auth'] = true;
			return print json_encode($arr);
		}
		
		if(array_key_exists('restore_pass', $_POST) ){	
			$userdata = array();
			$arr = array();			
			$userdata['restore_pass'] = stripslashes(strip_tags(trim($_POST['restore_pass'])));
			
			$arr["new_password"] = $users_obj->new_password($userdata['restore_pass']);
			return print json_encode($arr);		
		}
		
		if(array_key_exists('payd', $_POST) && array_key_exists('id_child', $_POST)){
			$userdata = array();
			$arr = array();			
			$userdata['payd'] = stripslashes(strip_tags(trim($_POST['payd'])));
			$userdata['id_child'] = (int)$_POST['id_child'];
			
			$arr['payd'] = $gamers_obj->payd($userdata['id_child']);
			return print json_encode($arr);		
		}
		
		if(array_key_exists('confirm', $_GET) && array_key_exists('ulogin', $_GET)){
			$userdata = array();
			$arr = array();			
			$userdata['confirm'] = stripslashes(strip_tags(trim($_GET['confirm'])));
			$userdata['ulogin'] = stripslashes(strip_tags(trim($_GET['ulogin'])));
			
			$arr["confirm"] =($users_obj->RegisterUser($userdata['ulogin'],$userdata['ulogin']));
			header("location:../index.php");	
		}
		
		if (array_key_exists('del_page',$_POST)){
			$userdata = array();
			$arr = array();	
			$userdata['del_page'] = (int)$_POST['del_page'];
			if($admin_obj->delete_page($userdata['del_page']))
				$arr['delete_page'] = true;
			else
				$arr['delete_page'] = false;
			return print json_encode($arr);
		}
			
		if((array_key_exists('upd_page',$_POST)) && (array_key_exists('title_page',$_POST)) && (array_key_exists('in_menu',$_POST)) && (array_key_exists('content',$_POST))){
			$arr = array();	
			$arr['upd_page'] = $admin_obj->update_page(array(
				'id' => (int)$_POST['upd_page'],
				'title' => trim( stripslashes($_POST['title_page'])),					
				'content' => trim( stripslashes($_POST['content'])),						
				'in_menu' => trim( stripslashes($_POST['in_menu'])),
				'seo_title' => trim( stripslashes($_POST['seo_title'])),
				'seo_description' => trim( stripslashes($_POST['seo_description'])),
				'seo_keywords' => trim( stripslashes($_POST['seo_keywords']))));
			return print json_encode($arr);
		}
		
		if((array_key_exists('upd_in_menu',$_POST)) && (array_key_exists('id_',$_POST)) && (array_key_exists('in_menu',$_POST))){
			$arr = array();
			$id = (int)$_POST['id_'];
			$arr['upd_in_menu'] = $admin_obj->upd_in_menu($id,$_POST['in_menu']);
			return print json_encode($arr);
		}
		
		if(array_key_exists('add_title_page',$_POST) && array_key_exists('add_in_menu',$_POST) && array_key_exists('add_content',$_POST)){
			$arr = array();
			if ($admin_obj->add_page(array(
						'title' => trim( stripslashes($_POST['add_title_page'])),					
						'content' => trim( stripslashes($_POST['add_content'])),						
						'in_menu' => trim( stripslashes($_POST['add_in_menu'])),
						'author' => trim( stripslashes($_SESSION['user']['login'])),
						'creation_date' => time(),
						'seo_title' => trim( stripslashes($_POST['seo_title'])),
						'seo_description' => trim( stripslashes($_POST['seo_description'])),
						'seo_keywords' => trim( stripslashes($_POST['seo_keywords']))
						)))
				$arr['add_page'] = true;
			else
				$arr['add_page'] = false;
			$arr['idpage'] = $admin_obj->select_idpage($_POST['add_title_page']);
			return print json_encode($arr);
		}
		
		if (array_key_exists('del_user',$_POST)){
			$arr = array();
			if($admin_obj->delete_user((int)$_POST['del_user']))			
				$arr['delete_user'] = true;
			else
				$arr['delete_user'] = false;
			return print json_encode($arr);
		}

		if(array_key_exists('upd_user',$_POST) && array_key_exists('upd_name',$_POST)&& array_key_exists('upd_surname',$_POST) && array_key_exists('upd_phone',$_POST) && array_key_exists('upd_country',$_POST)
		&& array_key_exists('upd_city',$_POST) && array_key_exists('upd_question',$_POST)&& array_key_exists('upd_response',$_POST) && array_key_exists('upd_privilege',$_POST) && array_key_exists('upd_payd',$_POST)){
			$userdata = array();
			$arr = array();	
			$userdata['upd_user'] = (int)$_POST['del_page'];
			$userdata['upd_name'] = stripslashes(strip_tags(trim($_POST['upd_name'])));
			$userdata['upd_surname'] = stripslashes(strip_tags(trim($_POST['upd_surname'])));
			$userdata['upd_phone'] = stripslashes(strip_tags(trim($_POST['upd_phone'])));
			$userdata['upd_country'] = stripslashes(strip_tags(trim($_POST['upd_country'])));
			$userdata['upd_city'] = stripslashes(strip_tags(trim($_POST['upd_city'])));
			$userdata['upd_question'] = stripslashes(strip_tags(trim($_POST['upd_question'])));
			$userdata['upd_response'] = stripslashes(strip_tags(trim($_POST['upd_response'])));
			$userdata['privilege'] = stripslashes(strip_tags(trim($_POST['privilege'])));
			
			$arr['update_user'] = $admin_obj->update_user(array(
				'id' => $userdata['upd_user'],
				'name' => $userdata['upd_name'],					
				'surname' => $userdata['upd_surname'],						
				'phone' => $userdata['upd_phone'],						
				'country' => $userdata['upd_country'],
				'city' => $userdata['upd_city'],					
				'question' => $userdata['upd_question'],						
				'response' => $userdata['upd_response'],
				'privilege' => $userdata['privilege']
			));
			$arr['payd'] = $gamers_obj->payd(trim( stripslashes($_POST['upd_user'])),trim( stripslashes($_POST['upd_payd'])));
			return print json_encode($arr);
		}
		//here
		if ((array_key_exists('id_child',$_POST)) && (array_key_exists('id_parent',$_POST))){
			$arr["is_gamer"] = false;
			$arr["gamer"] = false;
			$id_child = (int)$_POST['id_child'];
			$id_parent = (int)$_POST['id_parent'];
			if ($gamers_obj->is_gamer(($id_child) || ($_SESSION['user']['privilege']!=0))){
				$arr["is_gamer"] = true;			
				return print json_encode($arr);				
			} else {
				if (($gamers_obj->select_parent(($id_parent),$id_child))){
					$arr["gamer"] = true;
					$_SESSION['user']['privilege'] = 1;
					$_SESSION['user']['status'] = 2;
					return print json_encode($arr);
				}			
			}	
			return false;
		}
		
		if (array_key_exists('new_pass',$_POST) && array_key_exists('password_upd',$_POST) && array_key_exists('old_password',$_POST)){
			$arr['new_pass'] = $users_obj->update_pass($_SESSION['user']['id'],$_POST['old_password'],$_POST['password_upd']);
			return print json_encode($arr);
		}
		
		if (array_key_exists('login',$_POST) && array_key_exists('phone',$_POST) && array_key_exists('email',$_POST)
			&& array_key_exists('country',$_POST) && array_key_exists('city',$_POST) 
			&& array_key_exists('add_other_user',$_POST) && array_key_exists('distr',$_POST)
		){
			$arr['add_other_user'] = $users_obj->add_other_user($_POST['distr'],array(
			
						'login' => trim( stripslashes($_POST['login'])),
						'name' => trim( stripslashes($_POST['name'])),
						'surname' => trim( stripslashes($_POST['surname'])),
						'phone' => trim( stripslashes($_POST['phone'])),
						'email' => trim( stripslashes($_POST['email'])),
						'country' => trim( stripslashes($_POST['country'])),
						'city' => trim( stripslashes($_POST['city']))
						
			));
			
			/*$arr['new_pass'] = $users_obj->update_pass($_SESSION['user']['id'],$_POST['old_password'],$_POST['password_upd']);*/
			return print json_encode($arr);
			
		}
		
		if (array_key_exists('parent_id',$_POST) && array_key_exists('add_parent',$_POST)
		){
			
			$arr[] = array();
			$arr['ids'] = 'no_child';
			$arr['addParent'] = $gamers_obj->auto_select($_POST['parent_id'],$_SESSION['user']['id']);
			//var_dump($arr['addParent']);
			if ($arr['addParent']){
				$_SESSION['user']['privilege'] = 1;
				$_SESSION['user']['status'] = $arr['addParent'];
			}
			else{
				$arr['ids'] = $gamers_obj->select_free_child($_POST['parent_id']);
			}
			return print json_encode($arr);
		}
		
		if (array_key_exists('parent_id',$_POST) && array_key_exists('admin_add_parent',$_POST) && array_key_exists('user_id',$_POST)){
			/*$arr['addParent'] = $gamers_obj->auto_select($_POST['parent_id'],$_POST['user_id']);
			return print json_encode($arr);*/
			//$arr[] = array();
			$arr['addParent'] = $gamers_obj->auto_select($_POST['parent_id'],$_POST['user_id']);
			if ($arr['addParent']){
				/*$_SESSION['user']['privilege'] = 1;
				$_SESSION['user']['status'] = $arr['addParent'];*/
			}
			else{
				$arr['ids'] = $gamers_obj->select_free_child($_POST['parent_id']);
			}
			return print json_encode($arr);
		}
		
		if (array_key_exists('distribute',$_POST) && array_key_exists('distr',$_POST)
			&& array_key_exists('login',$_POST) && array_key_exists('email',$_POST)){
			$arr['distrParent'] = $gamers_obj->distribute_child($_POST['distr'],$_POST['login'],$_POST['email']);
			return print json_encode($arr);
		}
		
		if (array_key_exists('money',$_POST) && array_key_exists('amount_money',$_POST) && array_key_exists('id',$_POST)){			
			$arr['check_wait_money'] = $gamers_obj->check_wait_money($_SESSION['user']['id'],$_POST['amount_money']);
			return print json_encode($arr);
		}
		
		if (array_key_exists('send_complain',$_POST) && array_key_exists('message',$_POST)){
			$arr['send_complain'] = $users_obj->letter_complain($_SESSION['user']['id'],$_SESSION['user']['email'],$_POST['message']);
			return print json_encode($arr);
		}
		
		if (array_key_exists('send_message',$_POST) && array_key_exists('text',$_POST) && array_key_exists('id_to',$_POST)){
			$arr['user_message'] = $users_obj->user_message($_SESSION['user']['id'],$_POST['id_to'],$_POST['theme'],$_POST['text']);
			return print json_encode($arr);
		}
		
		if (array_key_exists('toReaded',$_POST) && array_key_exists('id_mes',$_POST)){
			$data_mes = $users_obj->data_message($_POST['id_mes']);
			if (($data_mes['to'] == $_SESSION['user']['id']) && ($data_mes['toReaded'] == 0))
				$arr['toReaded'] = $users_obj->toReaded($_POST['id_mes']);
			return print json_encode($arr);
		}
		
		if (array_key_exists('move_to_trash',$_POST) && array_key_exists('id_mes',$_POST)){
			$id = explode(",", $_POST['id_mes']);
			$counter = 0;
			$n = count($id);
			for ($i = 0; $i < $n; $i++){
				if ($users_obj->move_to_trash($_SESSION['user']['id'], $id[$i]))
					$counter++;
			}
			if ($counter == $n)
				$arr['move_to_trash'] = true;
			else
				$arr['move_to_trash'] = false;
			return print json_encode($arr);
		}
		
		if (array_key_exists('return_from_trash',$_POST) && array_key_exists('id_mes',$_POST)){
			$id = explode(",", $_POST['id_mes']);
			$counter = 0;
			$n = count($id);
			for ($i = 0; $i < $n; $i++){
				if ($users_obj->return_from_trash($_SESSION['user']['id'], $id[$i]))
					$counter++;
			}
			if ($counter == $n)
				$arr['return_from_trash'] = true;
			else
				$arr['return_from_trash'] = false;
			return print json_encode($arr);
		}
		
		if (array_key_exists('del_message',$_POST) && array_key_exists('id_mes',$_POST)){			
			$id = explode(",", $_POST['id_mes']);
			$counter = 0;
			$n = count($id);
			for ($i = 0; $i < $n; $i++){

				if ($users_obj->del_from_trash($_SESSION['user']['id'], $id[$i]))
					$counter++;
					//var_dump($users_obj->return_from_trash($_SESSION['user']['id'], $id[$i]));
			}
			if ($counter == $n)
				$arr['del_message'] = true;
			else
				$arr['del_message'] = false;
			return print json_encode($arr);
		}
		
		if (array_key_exists('upd_popup',$_POST) && array_key_exists('id',$_POST)){
			$arr[] = array();
			$about_gamer[] = array();
			$about_gamer = $gamers_obj->about_gamer($_POST['id']);
			
			$arr = $admin_obj->select_user($_POST['id']);
			$arr['name_country'] = $this->value_country( $arr['country']);
			$arr['get_countries'] = $this->getCountries($arr['country']);
			$arr['value_city'] = $this->value_city($arr['country'], $arr['city']);
			$arr['value_question'] = $this->value_question($arr['question']);
			$arr['get_question'] = $this->getQuestion($arr['question']);
			$arr['is_payd'] = $gamers_obj->is_payd($_POST['id']);
			$arr['id_parent'] = $about_gamer['next_parent'];
			
			$arr['parent_layer'] = $about_gamer['layer'];
			return print json_encode($arr);
		}
		//here new function
		if (array_key_exists('id',$_POST) && array_key_exists('level_plus',$_POST)){
			$arr = array();
			$id = (int)$_POST['id'];
			$arr['plus'] = $gamers_obj->check_wait_money($id);
			return print json_encode($arr);
		}
		
		if (array_key_exists('id',$_POST) && array_key_exists('level_minus',$_POST)){
			$arr = array();
			$id = (int)$_POST['id'];
			$arr['minus'] = $gamers_obj->no_new_level($id);
			return print json_encode($arr);
		}
		
	}
	
	public function getQuestion($questionId){
	
		$query = "SELECT * FROM questions";
		$result=mysql_query($query) or die (mysql_error());
		$html = '';
		while ($question=mysql_fetch_array($result)){
			$selected = ($questionId == $question['id']) ? ' selected="selected"' : '';
			$html .= '<option value="'.$question['id'].'" '.$selected.'>'.$question['question'].'</option>';
		}
		return $html;
	}
	
	
	
	private function initVars(){
        $protocol = isset($_SERVER['HTTPS']) ? 'https' : 'http';
        $server = $_SERVER['HTTP_HOST'];
        $port = $_SERVER['SERVER_PORT'] != 80 ? ":{$_SERVER['SERVER_PORT']}" : '';
        $path = rtrim(dirname($_SERVER['SCRIPT_NAME']), '/');
		define('HTTP_HOST', "$protocol://$server$port$path");	
	}
	
	public function is_active($id){
		if ($query = mysql_query("UPDATE user SET `is_active` = '1' WHERE id = '".$id."'") or die(mysql_error()))return true;
		return false;
	}
	
	public function no_active($id){
		if ($query = mysql_query("UPDATE user SET `is_active` = '0' WHERE id = '".$id."'") or die(mysql_error()))return true;
		return false;
	}
	
	public function last_visited($id){
		if ($query = mysql_query("UPDATE user SET `last_visit` = '".time()."' WHERE id = '".$id."'") or die(mysql_error()))return true;
			return false;
	}
	
	public function visited($id){
		if ($query = mysql_query("UPDATE user SET `visit` = '".time()."' WHERE id = '".$id."'") or die(mysql_error()))return true;
			return false;
	}
	
	public function getCountries($countryId){
		$query="SELECT * FROM countries";
		$result=mysql_query($query) or die (mysql_error());
		$html = '';
		while ($countries=mysql_fetch_array($result)){		
			$selected = ($countryId == $countries['id']) ? ' selected="selected"' : '';
			$html .= '<option value="'.$countries['id'].'" '.$selected.'>'.$countries['name'].'</option>';
		}
		return $html;
	}
	
	public function value_country($val){
		$query = mysql_query("SELECT name FROM countries WHERE id = '".$val."'") or die(mysql_error());
		$result = mysql_fetch_array($query);
		return $result['name'];
	}
	
	public function value_city_show($val){
		$query = mysql_query("SELECT name FROM cities WHERE id = '".$val."'") or die(mysql_error());
		$result = mysql_fetch_array($query);
		return $result['name'];
	}
	
	public function value_city($country, $cityID){
		$result=mysql_query("SELECT * FROM cities WHERE country_id='".(int)$country."'") or die (mysql_error());
		$html = '';
		while ($city=mysql_fetch_array($result)){
			$selected = ($cityID == $city['id']) ? ' selected="selected"' : '';
			$html .= '<option value="'.$city['id'].'" '.$selected.'>'.$city['name'].'</option>';
		}
		return $html;
	}
	
	public function parent_city($country, $cityID){
		$result=mysql_query("SELECT * FROM cities WHERE id='".(int)$country."'") or die (mysql_error());
		$city=mysql_fetch_array($result);
		return $city['name'];
	}
	
	public function value_question($val){
		$query = mysql_query("SELECT question FROM questions WHERE id = '".$val."'") or die(mysql_error());
		$result = mysql_fetch_array($query);
		return $result['question'];
	}
	
	public function time_difference($date){
		$day =  floor($date/86400);
		$hour = $date%86400;
		$minutes = $hour%3600;
		$hour = floor($hour/3600);
		$seconds = $minutes%60;
		$minutes = floor($minutes/60);
		
		if (($day != 0) && ($hour != 0) && ($minutes != 0))
			$day.'дн. '.$hour.'час. '.$minutes.'мин. '.$seconds.'сек.';
		if (($day == 0) && ($hour == 0) &&($minutes == 0))
			return 	$seconds.'сек.';
		if (($day == 0) && ($hour == 0))
			return 	$minutes.'мин. '.$seconds.'сек. ';
		if ($day == 0)
			return $hour.'час. '.$minutes.'мин. '.$seconds.'сек.';		
	}
	
	public function data_validation($mean, $type){
		if ($type == 'email'){
			$check = "^[a-zA-Z0-9\._-]+@[a-zA-Z0-9\._-]+\.[a-zA-Z]{2,4}$";
			
		}
	}
	
}

$core_obj = new core();